//
//  UploadIdCardViewController.m
//  PigMarket
//
//  Created by mac on 2019/3/22.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#pragma mark --------- 上传身份证

#import "UploadIdCardViewController.h"
#import "TZImagePickerController.h"

@interface UploadIdCardViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
/**
 titleLab
 */
@property (nonatomic , strong) UILabel *titleLab;

/**
 正面背景框
 */
@property (nonatomic , strong) UIImageView *frontIDCardBg;
/**
 正面身份证图片
 */
@property (nonatomic , strong) UIImageView *uploadfrontImgView;

/**
 反面背景框
 */
@property (nonatomic , strong) UIImageView *reverseSideIDCardBg;
/**
 反面身份证图片
 */
@property (nonatomic , strong) UIImageView *uploadReverseSideImgView;

/**
 uploadBtn
 */
@property (nonatomic , strong) UIButton *uploadBtn;

/**
 frontOrReverseStatus
 */
@property (nonatomic , assign) BOOL isFrontPhoto;
/**
 photosArr
 */
@property (nonatomic , strong) NSMutableArray *photosArr;

@property (strong, nonatomic) UIImagePickerController *imagePicker;

@property (nonatomic, copy) NSString *frontImageName;

@property (nonatomic, copy) NSString *backImageName;


@end

@implementation UploadIdCardViewController


#pragma mark - view life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseNavView.line.hidden = YES;
    [self setUpView];
}


#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    
    self.view.backgroundColor = KWhiteColor;
    [self.view addSubview:self.titleLab];
    [self.view addSubview:self.frontIDCardBg];
    [self.frontIDCardBg addSubview:self.uploadfrontImgView];
    [self.view addSubview:self.reverseSideIDCardBg];
    [self.reverseSideIDCardBg addSubview:self.uploadReverseSideImgView];
    [self.view addSubview:self.uploadBtn];
}


- (void) initLayout {
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(26 + TJ_NavBarHeight);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(30);
    }];
    [self.frontIDCardBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(50);
        make.size.mas_equalTo(CGSizeMake(200 * [JTool getScreenWidthScale], 136 * [JTool getScreenWidthScale]));
    }];
    [self.uploadfrontImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.frontIDCardBg.mas_centerX);
        make.centerY.mas_equalTo(self.frontIDCardBg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150 * [JTool getScreenWidthScale], 100 * [JTool getScreenWidthScale]));
    }];
    [self.reverseSideIDCardBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.frontIDCardBg.mas_bottom).offset(60);
        make.size.mas_equalTo(CGSizeMake(200 * [JTool getScreenWidthScale], 136 * [JTool getScreenWidthScale]));
    }];
    [self.uploadReverseSideImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.reverseSideIDCardBg.mas_centerX);
        make.centerY.mas_equalTo(self.reverseSideIDCardBg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(150 * [JTool getScreenWidthScale], 100 * [JTool getScreenWidthScale]));
    }];
    [self.uploadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.reverseSideIDCardBg.mas_bottom).offset(60);
        make.right.mas_equalTo(-30);
        make.left.mas_equalTo(30);
        make.height.mas_equalTo(48 * [JTool getScreenWidthScale]);
    }];
}



- (void) initAction {
    
    UITapGestureRecognizer *frontTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frontTapped)];
    [self.frontIDCardBg addGestureRecognizer:frontTap];
    
    UITapGestureRecognizer *reverseSideTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(reverseSideTapped)];
    [self.reverseSideIDCardBg addGestureRecognizer:reverseSideTap];
    
    [self.uploadBtn addTarget:self action:@selector(sumbitCardInformation) forControlEvents:UIControlEventTouchUpInside];
}


- (void)initData {
    
    self.frontImageName = self.backImageName = nil;
}


- (void)jumpPhotoOperation{
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    kWeakSelf(self)
    UIAlertAction * cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakself presentViewController:weakself.imagePicker animated:YES completion:nil];
    }];
    UIAlertAction * photosAction = [UIAlertAction actionWithTitle:@"从相册中选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.allowsEditing = YES;
        [weakself presentViewController:picker animated:YES completion:nil];
    }];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:cameraAction];
    [alert addAction:photosAction];
    [alert addAction:cancelAction];
    [weakself presentViewController:alert animated:YES completion:nil];
    
}



#pragma mark - UIImagePicker Delegate

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    if (!image) {
        [self showHudWithMsg:@"请重新获取照片"];
    }else {
        
        if (self.photosArr.count >0) {
            [self.photosArr removeAllObjects];
        }
        
        [self.photosArr addObject:image];
        
        if (self.isFrontPhoto) {
            self.uploadfrontImgView.image = image;
        }else{
            self.uploadReverseSideImgView.image = image;
        }
        
        [self uploadIDCard];
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark - target action

//身份证前面
- (void)frontTapped{
    self.isFrontPhoto = YES;
    [self jumpPhotoOperation];
}


//身份证后面
- (void)reverseSideTapped{
    self.isFrontPhoto = NO;
    [self jumpPhotoOperation];
}

- (void)uploadIDCard{
    //上传图片接口请求
   
    [self showProgressView];
    @weakify(self);
    NSString *account = [UserInfoDataProvider sharedInstance].userInfo.phone;
    [[[PigMarketApi sharedInstance] uploadPhotosWithPhotosArr:self.photosArr account:account type:UpDateImageType_Other] subscribeNext:^(PhotosList * _Nullable uploadPhotosList) {
        @strongify(self);
        [self hideProgressView];
        Photos *photo = uploadPhotosList.imgList[0];
        if (self.isFrontPhoto) {
            self.frontImageName = photo.imgName;
        }else{
            self.backImageName = photo.imgName;
        }
        
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self hideProgressView];
    }];
}


- (void)sumbitCardInformation{
    
    if (! self.frontImageName.length) {
        
        [self showHudWithMsg:@"请上传身份证正面照片"];
        return;
    }
    
    if (! self.backImageName.length) {
        
        [self showHudWithMsg:@"请上传身份证反面照片"];
        return;
    }
    
    @weakify(self);
    [self showProgressView];
    [[[PigMarketApi sharedInstance] editRealNameByIdCardFace:self.frontImageName idCardCon:self.backImageName] subscribeNext:^(NSNull * _Nullable x) {
        @strongify(self);
        [self hideProgressView];
        [self.navigationController popViewControllerAnimated:YES];
        
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self hideProgressView];
    }];
}


#pragma mark - getter and setter
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor textBlackColor];
        _titleLab.font = [UIFont xxLargerFont];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.text = @"请上传您本人身份证正反面照片";
    }
    return _titleLab;
}


- (UIImageView *)frontIDCardBg {
    if (!_frontIDCardBg) {
        _frontIDCardBg = [[UIImageView alloc] init];
        _frontIDCardBg.image = [UIImage imageNamed:@"user_realname_uploadIDbg"];
        _frontIDCardBg.userInteractionEnabled = YES;
    }
    return _frontIDCardBg;
}


- (UIImageView *)uploadfrontImgView {
    if (!_uploadfrontImgView) {
        _uploadfrontImgView = [[UIImageView alloc] init];
        _uploadfrontImgView.image = [UIImage imageNamed:@"user_realname_uploadID"];
    }
    return _uploadfrontImgView;
}



- (UIImageView *)reverseSideIDCardBg {
    if (!_reverseSideIDCardBg) {
        _reverseSideIDCardBg = [[UIImageView alloc] init];
        _reverseSideIDCardBg.image = [UIImage imageNamed:@"user_realname_uploadIDbg"];
        _reverseSideIDCardBg.userInteractionEnabled = YES;
    }
    return _reverseSideIDCardBg;
}



- (UIImageView *)uploadReverseSideImgView {
    if (!_uploadReverseSideImgView) {
        _uploadReverseSideImgView = [[UIImageView alloc] init];
        _uploadReverseSideImgView.image = [UIImage imageNamed:@"user_realname_uploadreversesideID"];

    }
    return _uploadReverseSideImgView;
}



- (UIButton *)uploadBtn {
    if (!_uploadBtn) {
        _uploadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_uploadBtn setTitle:@"上传" forState:UIControlStateNormal];
        [_uploadBtn setBackgroundImage:[UIImage imageNamed:@"btn_nomal_bg"] forState:UIControlStateNormal];
    }
    return _uploadBtn;
}



- (UIImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        _imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        _imagePicker.allowsEditing = YES;
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}



- (NSMutableArray *)photosArr {
    if (!_photosArr) {
        _photosArr = [[NSMutableArray alloc] init];
    }
    return _photosArr;
}


@end
